/*
 *  Copyright (C) 2009  Peter Kist & Jan Ripke
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "PropertyFactory.h"

namespace galaxy {

	namespace world {

		static PropertyFactory * factory = (PropertyFactory *) 0L;

		PropertyFactory::PropertyFactory(void)
		{
		}

		PropertyFactory::~PropertyFactory(void)
		{
			PropertyMap::iterator i = properties.begin();
			while (i != properties.end()) {
				Property * prop = i->second;
				if (prop) delete prop;
                i++;
			}
            properties.clear();
		}

		Property * PropertyFactory::createProperty (std::string& name) {
			PropertyFactory * pf = PropertyFactory::singleton ();
            Property * prop = pf->properties[name];
            if (prop) return prop->clone ();
            return (Property *) 0L;
		}

        bool PropertyFactory::registerProperty (Property& property, std::string& name) {
			PropertyFactory * pf = PropertyFactory::singleton ();
			if (pf->properties.find (name) == pf->properties.end()) {
				// property not already registered
				Property * factoryObj = property.clone();
				if (factoryObj) {
					pf->properties[name] = factoryObj;
				}
				return true;
			}
			return false;
		}

		PropertyFactory * PropertyFactory::singleton () {
			if (! factory) {
				factory = new PropertyFactory ();
			}
			return factory;
		}
	}
}

